package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(userId);
    }

    @Override
    public void clean() {
        shoppingCartMapper.cleanByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .dishId(shoppingCartDTO.getDishId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .setmealId(shoppingCartDTO.getSetmealId())
                .build();
        if (shoppingCartDTO.getDishId() != null) {
            ShoppingCart tempShoppingCart = shoppingCartMapper
                    .getByShoppingCart(shoppingCart);
            if (tempShoppingCart != null) {
                tempShoppingCart.setNumber(tempShoppingCart.getNumber() + 1);
                shoppingCartMapper.setNumberById(tempShoppingCart);
                return;
            } else {
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
        } else if (shoppingCartDTO.getSetmealId() != null) {
            ShoppingCart tempShoppingCart = shoppingCartMapper.getByShoppingCart(shoppingCart);
            if (tempShoppingCart != null) {
                tempShoppingCart.setNumber(tempShoppingCart.getNumber() + 1);
                shoppingCartMapper.setNumberById(tempShoppingCart);
            } else {
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
        }
        shoppingCartMapper.add(shoppingCart);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .dishId(shoppingCartDTO.getDishId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .setmealId(shoppingCartDTO.getSetmealId())
                .build();
        ShoppingCart tempShoppingCart = shoppingCartMapper.getByShoppingCart(shoppingCart);
        if (tempShoppingCart != null) {
            Integer number = tempShoppingCart.getNumber()-1;
            if (number <= 0) {
                shoppingCartMapper.deleteById(tempShoppingCart.getId());
                return;
            }
            tempShoppingCart.setNumber(number);
            shoppingCartMapper.setNumberById(tempShoppingCart);
        }

    }
}
